﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class MarkRiserPipeCommand : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("ExternalCommandData", "ExternalCommandData is null!");
			}
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Transaction transaction = new Transaction(document, "添加共享参数");
			transaction.Start();
			MarkOperation markOperation = new MarkOperation();
			markOperation.AddAllShareParametes(application);
			transaction.Commit();
			markOperation.SetActiveDocAllLoadedTags(document);
			if (MarkRiserPipeCommand.hWndRevit == null)
			{
				MarkRiserPipeCommand.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			int wMsg = 7;
			MarkRiserPipeCommand.SendMessage(MarkRiserPipeCommand.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			List<ViewType> list = new List<ViewType>();
            list.Add((ViewType)1);
            list.Add((ViewType)2);
			if (!YJKRevitTools.ViewCheck(application.ActiveUIDocument, list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			FrmMarkRiserpipe frmMarkRiserpipe = new FrmMarkRiserpipe();
			frmMarkRiserpipe.Show(MarkRiserPipeCommand.hWndRevit);
			new MarkRiserPipeOperation(activeUIDocument, frmMarkRiserpipe)
			{
				UIApp = application
			}.Execute();
			markOperation.SetActiveDocAllLoadedTags(document);
			frmMarkRiserpipe.Close();
			return 0;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
